package com.service.admin.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.admin.order.controller.rep.OrderProductDetailsRep;
import com.service.core.exception.CommonException;
import com.service.dao.mapper.OrderProductOtherDetailsMapper;
import com.service.dao.model.OrderProductOtherDetails;
import com.service.admin.order.service.OrderProductOtherDetailsService;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年11月14日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class OrderProductOtherDetailServiceImpl extends ServiceImpl<OrderProductOtherDetailsMapper, OrderProductOtherDetails> implements OrderProductOtherDetailsService {

    @Override
    public List<OrderProductDetailsRep> getOrderProductDetailsByOrderId(Integer orderId) {

        List<OrderProductOtherDetails> orderProductOtherDetails = baseMapper.selectList(new QueryWrapper<OrderProductOtherDetails>()
                .lambda().eq(OrderProductOtherDetails::getOrderId, orderId));
        if (ObjectUtils.isEmpty(orderProductOtherDetails)) {
            throw new CommonException("此订单不存在订单详情");
        }

        return orderProductOtherDetails.stream().map(item -> {
            OrderProductDetailsRep resultItem =  ModelMapperUtil.getStrictModelMapper().map(item, OrderProductDetailsRep.class);

            resultItem.setProductCoin(item.getProductCoinsName());
            resultItem.setGiftCoins(item.getGiftCoinsName());
            resultItem.setFreightCoins(item.getFreightCoinsName());

            return resultItem;
        }).collect(Collectors.toList());
    }
}
